using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class login : MonoBehaviour 
{
    public Texture2D background;
    public GUISkin defaultSkin;
    public string nextScene = "";

    public static string user = "";

    private string pass = "", repass = "", mess = "", email = "";
    private bool register = false , savePass = false , clicked = false;

    private void OnGUI()
    {
        if (background != null)
            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), background);

        GUI.skin = defaultSkin;
        if (!register)
            GUI.Window(0, new Rect(Screen.width / 2 - 200, Screen.height / 2 - 100, 400, 200), windowFunc, "Login");
        else
            GUI.Window(0, new Rect(Screen.width / 2 - 200, Screen.height / 2 - 100, 400, 200), windowFunc, "Register");
    }

    private void Start()
    {
        try
        {
            TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/frontier/login.sav");
            user = read.ReadLine();
            string temp = read.ReadLine();
            if (temp == "true")
            {
                savePass = true;
                pass = read.ReadLine();
            }
            read.Close();
        }
        catch (Exception)
        {
            if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/frontier/login.sav"))
            {
                string[] files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/frontier/login.sav");
                foreach (string file in files)
                {
                    File.Delete(file);
                }
                TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/frontier/login.sav");
                write.WriteLine("");
                write.Close();
            }
            
        }
    }

    private void windowFunc(int id)
    {
        if (!clicked)
        {
            if (register)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("User: ", GUILayout.Width(100));
                user = GUILayout.TextField(user, GUILayout.Width(200));
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label("Pass: ", GUILayout.Width(100));
                pass = GUILayout.PasswordField(pass, "*"[0], GUILayout.Width(200));
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                GUILayout.Label("Re-Pass: ", GUILayout.Width(100));
                repass = GUILayout.PasswordField(repass, "*"[0], GUILayout.Width(200));
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                GUILayout.Label("Email: ", GUILayout.Width(100));
                email = GUILayout.PasswordField(email, "*"[0], GUILayout.Width(200));
                GUILayout.EndHorizontal();

                if (GUILayout.Button("Create", GUILayout.Width(170)))
                {
                    if (pass != repass)
                    {
                        mess = "The both passwords don't match!";
                        clicked = true;
                    }
                    else
                    {
                        WWWForm Regform = new WWWForm();
                        Regform.AddField("user", user);
                        Regform.AddField("pass", pass);
                        Regform.AddField("email", email);
                        Regform.AddField("hash", "8564dsa");

                        WWW w = new WWW("http://frontier.theunknownstudios.com/onlineScripts/register.php", Regform);

                        StartCoroutine(tryRegister(w));
                    }
                }
                if (GUILayout.Button("Back", GUILayout.Width(170)))
                {
                    register = false;
                }
            }
            else
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("User: ", GUILayout.Width(100));
                user = GUILayout.TextField(user, GUILayout.Width(200));
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                GUILayout.Label("Pass: ", GUILayout.Width(100));
                pass = GUILayout.PasswordField(pass, "*"[0], GUILayout.Width(200));
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                if (GUILayout.Button("Login", GUILayout.Width(170)))
                {
                    clicked = true;
                    TextWriter write = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/frontier/login.sav");
                    write.WriteLine(user);
                    if (savePass)
                    {
                        write.WriteLine("true");
                        write.WriteLine(pass);
                    }
                    else
                    {
                        write.WriteLine("false");
                    }
                    write.Close();

                    WWWForm loginForm = new WWWForm();
                    loginForm.AddField("user", user);
                    loginForm.AddField("pass", pass);
                    loginForm.AddField("hash", "8564dsa");
                    WWW w = new WWW("http://frontier.theunknownstudios.com/onlineScripts/login.php", loginForm);

                    StartCoroutine(trylogin(w));
                }
                savePass = GUILayout.Toggle(savePass, "Save Password");
                GUILayout.EndHorizontal();
                if (GUILayout.Button("Register", GUILayout.Width(170)))
                {

                    register = true;
                }
                if (GUILayout.Button("Quit", GUILayout.Width(170)))
                {
                    Application.Quit();
                }
            }
        }
        else
        {
            GUILayout.Label(mess);
            if (mess != "")
                if (GUILayout.Button("Back"))
                {
                    clicked = false;
                    mess = "";
                }
        }
    }

    IEnumerator trylogin(WWW w)
    {
        yield return w;

        if (w.error == null)
        {
            if (w.text == "**login-Sccess**")
            {
                mess = "Login Success!";
                clicked = false;
                gameManager.GetUserData();
                Application.LoadLevel(nextScene);
            }
        }
        else
        {
            print(w.text);
        }
    }

    IEnumerator tryRegister(WWW w)
    {
        yield return w;

        if (w.error == null)
            mess = w.text;
        else
            mess = w.error;
    }
}
